//
//  SoundEngine.m
//  SIPphone
//
//  Created by wrt on 13-11-6.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "SoundEngine.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation SoundEngine

static SoundEngine *soundEngine = 0;

- (id)init
{
	if( self = [super init])
	{
		playerIncomingTone = 0;
		playerRingBackTone = 0;
	}
	return self;
}

-(bool) LoudSpeaker:(bool)bOpen
{
	//return false;
	UInt32 route;
	OSStatus error;
	UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;    // 1
	
	error = AudioSessionSetProperty (
                                     kAudioSessionProperty_AudioCategory,                        // 2
                                     sizeof (sessionCategory),                                   // 3
                                     &sessionCategory                                            // 4
                                     );
	
	route = bOpen?kAudioSessionOverrideAudioRoute_Speaker:kAudioSessionOverrideAudioRoute_None;
	error = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
    
	if(error == kAudioSessionNotInitialized)
	{
		NSLog(@"not init");
	}
	else if(kAudioSessionAlreadyInitialized)
	{
		NSLog(@"not init");
	}
	else if(kAudioSessionInitializationError)
	{
		NSLog(@"not init");
	}
	else if(kAudioSessionUnsupportedPropertyError)
	{
		NSLog(@"not init");
	}
	else if(kAudioSessionBadPropertySizeError)
	{
		NSLog(@"not init");
	}
	else if(kAudioSessionBadPropertySizeError)
	{
		NSLog(@"not init");
	}
	
	if(error!=kAudioServicesNoError)
	{
		NSLog(@"error");
	}
	return true;
}

+ (SoundEngine*) shareSound
{
	if (!soundEngine)
		soundEngine = [[SoundEngine alloc] init];
	return soundEngine;
}

-(bool) PlayIncomingTone
{
	if(!playerIncomingTone)
	{
		NSString *path = [[NSBundle mainBundle] pathForResource:@"iPhoneRing" ofType:@"mp3"];
		NSURL *url = [NSURL fileURLWithPath:path];
		NSError *error;
		playerIncomingTone = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
		if (playerIncomingTone == nil)
		{
			NSLog(@"Failed to create audio player (RingTone): %@", error);
			return false;
		}
	}
	if(playerIncomingTone.playing)
		return true;
	playerIncomingTone.numberOfLoops = -1;
	//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
	return [playerIncomingTone play];
}
-(void) StopIncomingTone
{
	if(playerIncomingTone && playerIncomingTone.playing){
		[playerIncomingTone stop];
	}
}
-(bool) PlayRingBackTone
{
	if(!playerRingBackTone)
	{
		NSString* path = [[NSBundle mainBundle] pathForResource:@"outgoingcall" ofType:@"wav"];
		NSURL *url = [NSURL fileURLWithPath:path];
		NSError *error;
		playerRingBackTone = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
		if (playerRingBackTone == nil)
		{
			NSLog(@"Failed to create audio player (RingBackTone): %@", error);
			return false;
		}
	}
	if(playerRingBackTone.playing)
		return true;
	playerRingBackTone.numberOfLoops = -1;
	return [playerRingBackTone play];
}
-(void) StopRingBackTone
{
	if(playerRingBackTone && playerRingBackTone.playing){
		[playerRingBackTone stop];
	}
}

- (void)PlayMessageTone
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"complete" ofType:@"wav"];
    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate=self;
    [theAudio play];
}

- (void)dealloc {
	
	if(playerRingBackTone)
    {
		if(playerRingBackTone.playing)[playerRingBackTone stop];
		[playerRingBackTone release];
		playerRingBackTone = 0;
	}
	
	if(playerIncomingTone)
    {
		if(playerIncomingTone.playing)[playerIncomingTone stop];
		[playerIncomingTone release];
		playerIncomingTone = 0;
	}
    [super dealloc];
}

- (void)returnSetCategory
{
    UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
	AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
    
    AVAudioSession *audioSession = [[AVAudioSession alloc] init];
    [audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
}

#pragma mark AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
	NSLog(@"Audio finish playing.");
    [player release];
}


@end
